﻿// See https://aka.ms/new-console-template for more information
using SqlSugar;
using static System.Reflection.Metadata.BlobBuilder;

Console.WriteLine("Hello, World!");

ConnectionConfig connectionConfig = new ConnectionConfig()
{
    ConnectionString = CustomConnectionConfig.ConnectionString001,
    IsAutoCloseConnection = true,
    DbType = DbType.SqlServer
};
using (SqlSugarClient db = new SqlSugarClient(connectionConfig))
{
    db.Aop.OnLogExecuting = (s, p) =>
    {
        Console.WriteLine("----------------------------");
        Console.WriteLine($"Sql语句:{s}");
    };

    var list2 = db.Queryable<StudentA>()
             //查2层
             .Includes(st => st.SchoolA, sch => sch.RoomList)//查询2级(等于EF ThenInclude)
                                                             //查1层
             .Includes(st => st.Books,au=>au.author)
             .ToList();


    var listRes = db.Queryable<StudentA>()

                         .Includes(a => a.SchoolA)
         
                         .Includes(a => a.Books, t => t.author).ToList();


}
public class StudentA
{
    [SugarColumn(IsPrimaryKey = true)]
    public int StudentId { get; set; }
    public string Name { get; set; }
    public int SchoolId { get; set; }
    [Navigate(NavigateType.OneToOne, nameof(SchoolId))]//一对一
    public SchoolA SchoolA { get; set; }
    [Navigate(NavigateType.OneToMany, nameof(BookA.studenId))]//一对多
    public List<BookA>? Books { get; set; }//只能是null不能赋默认值

}
public class SchoolA
{
    [SugarColumn(IsPrimaryKey = true)]
    public int SchoolId { get; set; }
    public string SchoolName { get; set; }
    [Navigate(NavigateType.OneToMany, nameof(RoomA.SchoolId))]//一对多
    public List<RoomA> RoomList { get; set; }//只能是null不能赋默认值
}


public class Author{
    public int BookId { get; set; }
    public string Name { get; set; }

}

public class RoomA
{
    [SugarColumn(IsPrimaryKey = true)]
    public int RoomId { get; set; }
    public string RoomName { get; set; }
    public int SchoolId { get; set; }
}
public class BookA
{
    [SugarColumn(IsPrimaryKey = true)]
    public int BookId { get; set; }

    public string Name { get; set; }
    public int studenId { get; set; }
    [Navigate(NavigateType.OneToOne, nameof(Author.BookId))]//一对多
    public Author author { get; set; }
}


public class CustomConnectionConfig
{
    /// <summary>
    /// 主库操作字符串
    /// </summary>
    public static string ConnectionString001 = "Data Source=PC-202206030027;Initial Catalog=ZhaoxiSqlSugarDb;User ID=sa;Password=sa123";

    /// <summary>
    /// CodeFirst-专用字符串
    /// </summary>
    public static string ConnectionString002 = "Data Source=PC-202206030027;Initial Catalog=ZhaoxiSqlSugarDb_Info;User ID=sa;Password=sa123";

    /// <summary>
    /// 跨库查询字符串  //ZhaoxiSqlSugarDb_Item
    /// </summary>
    public static string ConnectionString003 = "Data Source=PC-202206030027;Initial Catalog = ZhaoxiSqlSugarDb_Item; User ID = sa; Password=sa123";


    /// <summary>
    /// 测试分库的链接字符串，如果要修改--修改PC-202206030027   和 用户名密码即可
    /// </summary>
    public static string ConnectionString004 = "Data Source=PC-202206030027;Initial Catalog = ZhaoxiSqlSugarDb_Item_2; User ID = sa; Password=sa123";

    /// <summary>
    /// 读写分离中的从库链接
    /// </summary>
    public static string ConnectionStringRead01 = "Data Source=PC-202206030027;Initial Catalog=ZhaoxiSqlSugarDb_Read01;User ID=sa;Password=sa123";

    /// <summary>
    ///  读写分离中的从库链接
    /// </summary>
    public static string ConnectionStringRead02 = "Data Source=PC-202206030027;Initial Catalog=ZhaoxiSqlSugarDb_Read02;User ID=sa;Password=sa123";

}

